package ai.people.netmon.framework.domain.archives.entity;

import ai.people.netmon.framework.domain.archives.vo.ArchivesDetailVO;
import ai.people.netmon.framework.domain.archives.vo.ArchivesVO;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.annotation.FieldStrategy;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 *
 * 社交媒体
 *
 * @author zhenglin
 * @since 2022-03-30
 */
@Data
@EqualsAndHashCode(callSuper = false)
@NoArgsConstructor
@TableName(schema = "webservice",value = "person_archives_social_info")
public class ArchivesSocialInfo implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 主键ID	
     */
    @TableId
    private Long id;

    /**
     * 社交媒体
     */
    @TableField(updateStrategy = FieldStrategy.IGNORED)
    private String socialInfo;

    /**
     * 电子邮箱
     */
    @TableField(updateStrategy = FieldStrategy.IGNORED)
    private String emailInfo;

    /**
     * 钱包地址
     */
    @TableField(updateStrategy = FieldStrategy.IGNORED)
    private String walletInfo;

    /**
     * 数据泄露
     */
    @TableField(updateStrategy = FieldStrategy.IGNORED)
    private String dataLeakage;

    /**
     * 基本信息ID
     */
    private Long baseInfoId;


    public ArchivesSocialInfo(ArchivesVO.SocialInfo socialInfo, Long baseInfoId){
        this.socialInfo = socialInfo.socialInfosJsonStr();
        this.emailInfo = socialInfo.emailInfoJsonStr();
        this.walletInfo = socialInfo.walletInfoJsonStr();
        this.dataLeakage = socialInfo.getDataLeakage();
        this.baseInfoId = baseInfoId;
    }

    public ArchivesSocialInfo(Long baseInfoId) {
        this.baseInfoId = baseInfoId;
    }

    public List<ArchivesVO.SocialInfo.SocialInfoInner> buildSocialInfo(){
        if(StringUtils.isNotBlank(this.socialInfo)){
            return JSONArray.parseArray(this.socialInfo,ArchivesVO.SocialInfo.SocialInfoInner.class);
        }
        return Collections.emptyList();
    }

    public List<ArchivesDetailVO.SocialInfo.Info> buildSocialDetailInfo(){
        if(StringUtils.isNotBlank(this.socialInfo)){
            List<ArchivesVO.SocialInfo.SocialInfoInner> socialInfoInners = JSONArray.parseArray(this.socialInfo, ArchivesVO.SocialInfo.SocialInfoInner.class);
            Map<String, List<ArchivesVO.SocialInfo.SocialInfoInner>> collect = socialInfoInners.stream().collect(Collectors.groupingBy(ArchivesVO.SocialInfo.SocialInfoInner::getSocialPlatform));
            return collect.entrySet().stream().map(entry -> {
                ArchivesDetailVO.SocialInfo.Info info = new ArchivesDetailVO.SocialInfo.Info();
                info.setSocialPlatform(entry.getKey());
                info.setSocialInfoInners(entry.getValue());
                return info;
            }).collect(Collectors.toList());
        }
        return Collections.emptyList();
    }

    public List<ArchivesVO.SocialInfo.SocialInfoInner> buildSocialDetailInfo2(){
        if(StringUtils.isNotBlank(this.socialInfo)){
            return JSONArray.parseArray(this.socialInfo, ArchivesVO.SocialInfo.SocialInfoInner.class);
        }
        return Collections.emptyList();
    }

    public List<ArchivesVO.SocialInfo.Email> buildSocialEmail(){
        if(StringUtils.isNotBlank(this.emailInfo)){
            return JSONArray.parseArray(this.emailInfo,ArchivesVO.SocialInfo.Email.class);
        }
        return Collections.emptyList();
    }

    public List<ArchivesVO.SocialInfo.Wallet> buildSocialWallet(){
        if(StringUtils.isNotBlank(this.walletInfo)){
            return JSONArray.parseArray(this.walletInfo,ArchivesVO.SocialInfo.Wallet.class);
        }
        return Collections.emptyList();
    }
}
